package com.chen.base.module.redis.config;

import cn.hutool.core.lang.Assert;
import com.chen.base.module.redis.annotation.RefreshCache;
import com.chen.base.module.redis.annotation.RefreshCacheParam;
import com.chen.base.utils.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: 陈锦龙
 * @Date: 2025/4/3 9:35
 */
@Slf4j
@Component
@ConditionalOnProperty(value = "enable.refresh-cache", havingValue = "true", matchIfMissing = true)
public class RefreshCacheJob {

    @Scheduled(cron = "0 0/30 * * * ?")
    public void run() throws Exception {
        Map<Object, Map<Method, RefreshCache>> beansMap = SpringContextHolder.getBeansByMethodAnnotate(RefreshCache.class);
        Map<Object, Map<Method, RefreshCacheParam>> paramBeansMap = SpringContextHolder.getBeansByMethodAnnotate(RefreshCacheParam.class);

        for (Map.Entry<Object, Map<Method, RefreshCache>> beanEntry : beansMap.entrySet()) {
            Object bean = beanEntry.getKey();
            Map<Method, RefreshCache> methodMap = beanEntry.getValue();

            for (Map.Entry<Method, RefreshCache> cacheEntry : methodMap.entrySet()) {
                Method method = cacheEntry.getKey();
                String refreshCache = cacheEntry.getValue().value();

                if (method.getParameterCount() > 0){
                    for (List<Object> args : getParam(refreshCache, paramBeansMap)) {
                        try {
                            String msg = "参数不合法! -> " + bean.getClass().getTypeName() + "." + method.getName() + "()";
                            Assert.isTrue(args.size() == method.getParameterCount(), msg);
                            method.invoke(bean, args.toArray());
                        }catch (Exception e){
                            log.error("刷新缓存异常: {}", e);
                        }
                    }
                }else {
                    method.invoke(bean);
                }
            }
        }
    }

    private List<List<Object>> getParam(String refreshCacheKey, Map<Object, Map<Method, RefreshCacheParam>> paramBeansMap) throws Exception {
        List<List<Object>> args = new ArrayList<>();
        for (Map.Entry<Object, Map<Method, RefreshCacheParam>> beanEntry : paramBeansMap.entrySet()) {
            Object bean = beanEntry.getKey();
            Map<Method, RefreshCacheParam> methodMap = beanEntry.getValue();

            for (Map.Entry<Method, RefreshCacheParam> paramEntry : methodMap.entrySet()) {
                Method method = paramEntry.getKey();
                RefreshCacheParam refreshCacheParam = paramEntry.getValue();
                if (refreshCacheKey.equals(refreshCacheParam.value())){
                    return (List<List<Object>>) method.invoke(bean);
                }
            }
        }

        return args;
    }
}
